package com.ssm.student.controller;

import com.ssm.student.eo.TeacherEoPage;
import com.ssm.student.model.Teacher;
import com.ssm.student.service.TeacherService;
import com.ssm.student.tool.ResponseMessage;
import com.ssm.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
        * 对外提供数据服务接口
        */
@Api(tags = "teacher")
@RestController
public class TeacherController {
         @Autowired
         private TeacherService teacherService;

         @ApiOperation(value = "教师添加方法")
         @RequestMapping(value = "/addTeacher", method = RequestMethod.GET)
         public ResponseMessage addTeacher(Teacher teacher)  {
             int IsAdd = 0;
             //传入数据的判断
             //(1)实体对象不能为空
             if (teacher != null) {
                 //(2)角色名称不能为空
                 if (teacher.getTno() != null) {
                     //(3)添加角色数据
                     IsAdd = teacherService.AddTeacher(teacher);
                     if (IsAdd == 1) {
                         return Result.success("保存成功");
                     } else if (IsAdd == -1) {
                         return Result.error("教师重复，不能添加");
                     } else {
                         return Result.error("保存失败");
                     }
                 }
             }
             return Result.error("保存失败");
         }

         @ApiOperation(value = "教师删除方法")
         @RequestMapping(value = "/delTeacher", method = RequestMethod.GET)
         public ResponseMessage delTeacher(int teacherid) {
             if (teacherid != 0) {
                 int delInt = 0;
                 delInt = teacherService.DeleteTeacher(teacherid);
                 if (delInt > 0) {
                     return Result.success("删除成功");
                 } else {
                     return Result.error("删除失败");
                 }
             } else {
                 return Result.error("删除的主键不能为0请重新输入");
             }
         }

         @ApiOperation(value = "角色修改方法")
         @RequestMapping(value = "/updateTeacher", method = RequestMethod.GET)
         public ResponseMessage updateTeacher(Teacher teacher)  {
             //传入的参数不能为空
             if (teacher != null) {
                 //参数中主键不能为0
                 if (teacher.getTeacherid() != 0) {
                     if (teacherService.UpdateTeacher(teacher) == 1) {
                         //
                         if (teacher.getTno()!=null){
                             if (teacher.getShoukefanwei().length()<5){
                                 return Result.success("更新成功");
                             }else {
                                 return Result.error("更新失败");
                             }
                         }else {
                             return Result.error("更新失败");
                         }

                     } else {
                         return Result.error("更新失败");
                     }
                 } else {
                     return Result.error("更新传入参数不正确");
                 }
             }

             return Result.error("更新传入参数不正确");
         }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getAllTeacher",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    private  ResponseMessage getAllTeacher(@RequestBody TeacherEoPage teacherEoPage){
        Map<String, List> map=new HashMap<>();
        List<TeacherEoPage> listteacher=teacherService.getWhereAll(teacherEoPage);
        map.put("list",listteacher);
        return Result.success("200",map);
    }
     }
